package com.zr.controller.sys

import com.alibaba.fastjson.JSON
import com.github.pagehelper.PageInfo
import com.zr.service.sys.impl.AuthServerImpl
import com.zr.service.sys.impl.RoleServerImpl
import com.zr.service.sys.impl.UserServerImpl
import com.zr.util.RedisUtils
import com.zr.util.Result
import com.zr.vo.sys.User
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import java.sql.DriverManager.println
import java.util.*
import java.util.concurrent.TimeUnit

@RestController
@RequestMapping("/sys/user")
class UserController {
    //自动绑定的标签千万不能忘了写，对于自动绑定的类使用@Autowired，对于接口，使用@Resource
    //只有正确地自动绑定了，才会在运行时创建实例
    @Autowired
    private val userServer:UserServerImpl?=null
    @Autowired
    private val authServer: AuthServerImpl?=null
    @Autowired
    private val roleServer:RoleServerImpl?=null
    @Autowired
    private val redisUtils: RedisUtils?=null
    @RequestMapping("/selectByUser")
    fun selectByUser(@RequestBody usr:User):String{
        val selectResult=userServer?.selectByUser(usr)
        //对每一个查询出来的user查询它的角色列表并将角色列表赋予这个角色。
        if(selectResult!=null){
            for(user in selectResult){
                //获取该用户的角色列表
                val userRoleList=roleServer?.selectRoleListByUserId(user.userId)
                user?.rolesList=userRoleList
            }
        }
        //使用pageInfo对查询到的结果进行封装，从而向前端提供分页数据
        val pageInfo=PageInfo<User>(selectResult)
        val result=Result(true,null,pageInfo)
        //return "${usr.pageIndex},${usr.pageNum}"
        return JSON.toJSONString(result)
    }
    @RequestMapping("/insert")
    fun insert(usr:User):String{
        userServer?.insert(usr)
        //如果插入成功就返回这些
        return JSON.toJSONString(Result(true,"Insert Success!",usr))
    }
    @RequestMapping("/login")
    fun login(@RequestBody usr: User):String{
        val loginUser=userServer?.login(usr)
        //获取该用户的权限列表
        val loginUserAuthList=authServer?.queryAuthByUserId(usr.userId)
        loginUser?.authUrlsList=loginUserAuthList
        //获取该用户的角色列表
        val loginUserRoleList=roleServer?.selectRoleListByUserId(usr.userId)
        loginUser?.rolesList=loginUserRoleList
        //获取该用户的头像
        val loginUserHeadImg=userServer?.getImgByUrl(loginUser?.imgUrl)
        loginUser?.base64=loginUserHeadImg
            if(loginUser!=null){
                //为当前用户随机生成一个token
                val token= UUID.randomUUID().toString()
                //将token存入redis缓存中
                val retult=redisUtils?.set(token,loginUser,15L, TimeUnit.MINUTES)
                //登录成功时直接返回Token
                return JSON.toJSONString(Result(true,token,loginUser))
            }
        return JSON.toJSONString(Result(false,"登录失败！",null))
    }
    @RequestMapping("/change")
    fun change(@RequestBody usr:User):String{
        userServer?.change(usr)
        return JSON.toJSONString(Result(true,"修改成功",null))
    }
    @RequestMapping("/add")
    fun add(@RequestBody usr:User):String{
        println(usr.toString())
        userServer?.add(usr)
        return JSON.toJSONString(Result(true,"添加成功",usr))
    }
    @RequestMapping("/edit")
    fun edit(@RequestBody usr:User):String{
        userServer?.edit(usr)
        return JSON.toJSONString(Result(true,"更新成功",null))
    }
    @RequestMapping("/del/{userId}")
    fun del(@PathVariable("userId") userId:String):String{
        userServer?.del(userId)
        return JSON.toJSONString(Result(true,"删除成功",null))
    }
    @RequestMapping("/uploadImg")
    fun uploadImg(file: MultipartFile):String{
        val base64=userServer?.uploadImg(file)
        return JSON.toJSONString(Result(true,"上传成功",base64))
    }
    @RequestMapping("/logout")
    fun logout():String{
        userServer?.logout()
        return JSON.toJSONString(Result(true,"退出成功",null))
    }



}